package com.thinkwide.data.net;

import com.thinkwide.data.ormapping.datatable.MFDataColumn;
import com.thinkwide.data.ormapping.datatable.MFDataColumnException;
import com.thinkwide.data.ormapping.datatable.MFDataRow;
import com.thinkwide.data.ormapping.datatable.MFDataTable;
import com.thinkwide.data.ormapping.enums.MFDataType;
import com.thinkwide.data.core.MFDate;
import com.thinkwide.data.core.MFString;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MFRequest {
    public final static String sysPrefix = "_";

    public static Map<String, String> getParameter(HttpServletRequest request) {
        return getParameter(request, true);
    }

    public static Map<String, String> getHeader(HttpServletRequest request) {
        Map<String, String> data = new LinkedHashMap<String, String>();
        Enumeration enu = request.getHeaderNames();
        while (enu.hasMoreElements()) {
            String headerName = (String) enu.nextElement();
            String headerValue = request.getHeader(headerName);
            data.put(headerName, headerValue);
            //MFDebug.println(headerName+":"+headerValue);
        }
        return data;
    }

    public static Map<String, String> getParameter(HttpServletRequest request, Boolean ignoreSys) {
        Enumeration names = request.getParameterNames();

        Map<String, String> paramMap = new LinkedHashMap<String, String>();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = request.getParameter(name);
            if (MFString.notNullOrEmpty(name) && ignoreSys)
                if (name.startsWith(sysPrefix)) continue;
            paramMap.put(name, value);
        }

        return paramMap;
    }


    public static String findValue(Map<String, String> map, String key) {
        for (Map.Entry<String, String> item : map.entrySet()) {
            if (MFString.compareNoCase(key, item.getKey())) {
                return item.getValue();
            }
        }

        return null;
    }

    public static List<String> findListValue(Map<String, List<String>> map, String key) {
        for (Map.Entry<String, List<String>> item : map.entrySet()) {
            if (MFString.compareNoCase(key, item.getKey())) {
                return item.getValue();
            }
        }

        return null;
    }

    public static Map<String, List<String>> getParameterList(HttpServletRequest request) {
        Enumeration names = request.getParameterNames();

        Map<String, List<String>> paramMap = new LinkedHashMap<String, List<String>>();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String[] values = request.getParameterValues(name);
            paramMap.put(name, Arrays.asList(values));
        }

        return paramMap;
    }


    public static void setDataRowValue(MFDataRow row, HttpServletRequest request) throws MFDataColumnException, ParseException {
        setDataRowValue(row, "", request);
    }

    public static void setDataRowValue(MFDataRow row, String prefix, HttpServletRequest request) throws MFDataColumnException, ParseException {
        String newPrefix = "";

        if (MFString.notNullOrEmpty(prefix))
            newPrefix = prefix + ".";

        MFDataTable table = row.getDataRows().getDataTable();
        Map<String, String> map = getParameter(request);

        for (MFDataColumn item : table.getColumns()) {

            String field = item.getField();
            //MFDebug.println(field);
            String data = findValue(map, newPrefix + item.getField());
            if (data != null) {
                if (item.getXdataType() == MFDataType.dtDate) {
                    if (MFString.isNullOrEmpty(data))
                        row.setValue(item.getField(), null);
                    else {
                        String str = MFString.notNullOrEmpty(item.getDateFormat()) ? item.getDateFormat() : "yyyy-MM-dd";
                        row.setValue(item.getField(), (Object) MFDate.parse(data, str));
                    }
                } else if (item.getXdataType() == MFDataType.dtDateTime) {
                    if (MFString.isNullOrEmpty(data))
                        row.setValue(item.getField(), null);
                    else {
                        //if (data instanceof Date){}
                        //String str=MFString.notNullOrEmpty(item.getDateFormat())?item.getDateFormat():"yyyy-MM-dd hh:mm:ss";
                        //row.setValue(item.getField(), (Object)MFDate.parse(data,str ));
                        row.setValue(item.getField(), data);
                    }
                } else if (item.getXdataType() == MFDataType.dtFloat ||
                        item.getXdataType() == MFDataType.dtNumber ||
                        item.getXdataType() == MFDataType.dtMoney ||
                        item.getXdataType() == MFDataType.dtInt ||
                        item.getXdataType() == MFDataType.dtGuid ||
                        item.getXdataType() == MFDataType.dtBit) {
                    if (MFString.isNullOrEmpty(data))
                        row.setValue(item.getField(), null);
                    else
                        row.setValue(item.getField(), (Object) data);
                } else
                    row.setValue(item.getField(), (Object) data);
            }
        }

    }

    public static MFDataTable getParameter(String prefix, HttpServletRequest request) throws MFDataColumnException {
        return getParameter(prefix, request, true);
    }

    public static MFDataTable getParameter(String prefix, HttpServletRequest request, Boolean ignoreSys) throws MFDataColumnException {
        Enumeration names = request.getParameterNames();

        MFDataTable table = new MFDataTable();
        Map<String, String> paramMap = new LinkedHashMap<String, String>();

        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();

            if (name.startsWith(prefix)) {
                if (MFString.notNullOrEmpty(name) && ignoreSys)
                    if (name.startsWith(sysPrefix)) continue;

                String shortName = name.substring(name.indexOf(".") + 1);
                String[] value = request.getParameterValues(name);
                if (!table.getColumns().hasColumn(shortName))
                    table.getColumns().addColumn(new MFDataColumn(shortName));

                int i = 0;
                for (String item : value) {
                    if (table.getRows().size() < (i + 1)) table.newRow();
                    table.getRows().get(i).setValue(shortName, item);
                    i++;
                }
            }

            //paramMap.put(name, value);
        }

        return table;
    }


}
